import subprocess
from src.core import log


def run(cmd, write=True):
    """运行命令"""
    result = subprocess.run(
        cmd,
        shell=True,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        universal_newlines=True,
    )
    if write:
        log.DEBUG(f"@运行命令: <结果码：{result.returncode}> {cmd}")
    if result.stdout.strip() in [""]:
        return result.stderr.strip()
    return result.stdout.strip()
